package com.example.tourstor.myself;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.tourstor.BaseBean;
import com.example.tourstor.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ASUS on 2018/9/13.
 */

public class CollectAdapter extends RecyclerView.Adapter<CollectAdapter.AuthorViewHolder> {

    private static final int MYLIVE_MODE_CHECK = 0;
    int mEditMode = MYLIVE_MODE_CHECK;

    private int secret = 0;
    private String title = "";
    private Context mContext;
    private LayoutInflater mInflater;
    private List<BaseBean> mBaseBeans;
    private OnItemClickListener mOnItemClickListener;

    public CollectAdapter(Context context, List<BaseBean> mBaseBeans) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        this.mBaseBeans = mBaseBeans;
    }

    //在activity中拿到数据添加进adapter中
    public void notifyAdapter(List<BaseBean> mBaseBeans, boolean isAdd) {
        if (!isAdd) {
            this.mBaseBeans = mBaseBeans;
        } else {
            this.mBaseBeans.addAll(mBaseBeans);
        }
        notifyDataSetChanged();
    }

    //判空
    public List<BaseBean> getMyBean() {
        if (mBaseBeans == null) {
            mBaseBeans = new ArrayList<>();
        }
        return mBaseBeans;
    }


    @Override
    public AuthorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new AuthorViewHolder(mInflater.inflate(R.layout.item_collect, parent, false));
    }

    @Override
    public void onBindViewHolder(final AuthorViewHolder holder, int position) {
        //这句话关掉IOS阻塞式交互效果 并依次打开左滑右滑
        //((SwipeMenuLayout)holder.itemView).setIos(false);//.setLeftSwipe(position % 2 == 0 ? true : false);

        ///holder.mNickNameView.setText(mBaseBeans.get(position).name + (position % 2 == 0 ? "北京大学" : "清华大学"));

        final BaseBean myBean = mBaseBeans.get(holder.getAdapterPosition());
        //////////////////////////可以换

        holder.mstoreImg.setImageResource(R.drawable.ic_launcher_background);  //商铺图片
        holder.mstoreName.setText(myBean.getStoreName());  //商铺名字

        //显示和隐藏
        if (mEditMode == MYLIVE_MODE_CHECK) {
            holder.mCheckBox.setVisibility(View.GONE);
        } else {
            holder.mCheckBox.setVisibility(View.VISIBLE);

            //然后点击条目选中和不选中的时候为Imageview设置不同的图片
            if (myBean.isSelect()) {
                holder.mCheckBox.setImageResource(R.drawable.ic_checked);
            } else {
                holder.mCheckBox.setImageResource(R.drawable.ic_unchecked);
            }
        }



        //?????????
        //注意事项，设置item点击，不能对整个holder.itemView设置咯，只能对第一个子View，即原来的content设置


        (holder.itemView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(holder.getAdapterPosition(), mBaseBeans);
            }
        });
        /*//验证长按
        holder.mRootView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int pos = holder.getLayoutPosition();
                Toast.makeText(mContext, "longclig", Toast.LENGTH_SHORT).show();
                mOnItemClickListener.onItemLongClick(-55, holder.itemView, pos);
                Log.d("TAG", "onLongClick() called with: v = [" + v + "]");
                return false;
            }
        });*/
    }

    //外部调用方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(int pos, List<BaseBean> mBaseBeans);
    }

    public void setEditMode(int editMode) {
        mEditMode = editMode;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return null != mBaseBeans ? mBaseBeans.size() : 0;
    }


    class AuthorViewHolder extends RecyclerView.ViewHolder {

        //@BindView(R.id.collect_root_view)
        RelativeLayout mCollectRootView;


        //@BindView(R.id.store_img)
        ImageView mstoreImg;
        //@BindView(R.id.store_name)
        TextView mstoreName;

        //@BindView(R.id.check_box)
        ImageView mCheckBox;


        public AuthorViewHolder(View itemView) {
            super(itemView);

            //ButterKnife.bind(this, itemView);
            mCollectRootView = itemView.findViewById(R.id.collect_root_view);
            mstoreImg = itemView.findViewById(R.id.store_img);
            mstoreName = itemView.findViewById(R.id.store_name);
            mCheckBox = itemView.findViewById(R.id.check_box);



        }
    }
}
